package com.bjsdzk.collector.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bjsdzk.collector.one2one.CollectorServer;

public class PropertyUtil {

	private static Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
	    private static Properties props;
	    static{
	        loadProps();
	    }

	    synchronized static private void loadProps(){
	        props = new Properties();
	        InputStream inApp = null;
	        InputStream in = null;
	        try {
//	　　　　　　　<!--第一种，通过类加载器进行获取properties文件流-->
	        	inApp = PropertyUtil.class.getClassLoader().getResourceAsStream("application.properties");
	            props.load(inApp);
	            in = PropertyUtil.class.getClassLoader().getResourceAsStream("application-"+props.getProperty("spring.profiles.active")+".properties");
	            props.load(in);
	        } catch (FileNotFoundException e) {
	            logger.error("jdbc.properties文件未找到");
	        } catch (IOException e) {
	            logger.error("出现IOException");
	        } finally {
	            try {
	                if(null != in) {
	                    in.close();
	                }
	                if(null != inApp) {
	                    in.close();
	                }
	            } catch (IOException e) {
	                logger.error("jdbc.properties文件流关闭出现异常");
	            }
	        }
//	        logger.info("properties文件内容：" + props);
	    }

	    public static String getProperty(String key){
	        if(null == props) {
	            loadProps();
	        }
	        return props.getProperty(key);
	    }

	    public static String getProperty(String key, String defaultValue) {
	        if(null == props) {
	            loadProps();
	        }
	        return props.getProperty(key, defaultValue);
	    }
}
